<?
include "connectDB.php";

$category = 'Computer_science';
mysql_query("insert into relation(concept,parent, visited, level) values('Computer_science','Computer_science',1,0)");
$level = 0;
while(1){
$query = "select cl_to, page_namespace, page_title from categorylinks join page on page_id = cl_from where cl_to = '$category' and page_namespace = 14";
$result=mysql_query($query);

while($val = mysql_fetch_array($result)){
$concept = $val['page_title'];
$parent = $val['cl_to'];
//echo $val['page_title'].'<br>';

$q3 = "select level from relation where concept = '$parent' limit 1";
$r3 = mysql_query($q3) or die(mysql_error());
while($l3 = mysql_fetch_array($r3))
$level = intval($l3[0])+1;

$query2 = "insert into relation(concept,parent, visited, level) values('$concept','$parent',0,'$level')";
$result1 = mysql_query($query2);//  or die(mysql_error());
}

$query4 = "update relation set visited = 1 where concept = '$category' and visited = 0 limit 1" ;
//echo '<br>'.$query4;
$result4 = mysql_query($query4);//or print(mysql_error());

$query3 = "select * from relation where visited = 0 limit 1";
$result3 = mysql_query($query3);//or print(mysql_error());
if(!mysql_num_rows($result3)) break;
$val3 = mysql_fetch_array($result3);
$category = mysql_escape_string($val3['concept']);


}
?>